﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aras.IOM;

namespace ArasConnection
{
    public static  class ArasFunction
    {
        /// <summary>
        /// 根据品号检查PLM Part中是否存在相关信息
        /// </summary>
        /// <param name="pno">货品品号</param>
        /// <returns>True 存在，false 不存在</returns>
        public static string checkPartByPrdNO(string pno)
        {
            string isHaving = "<AML><Item type=\"Part\" action=\"get\" where=\"[Part].item_number=\'" + pno + "\' \"></Item></AML>";
            Item haveItem = PublicState.ArasLoginInnovator.applyAML(isHaving);
            if (haveItem.isEmpty() || haveItem.isError())
            {
                return "";
            }
            else
            {
                return haveItem.getProperty("id").ToString();
            }
        }
        /// <summary>
        /// 将货品信息添加到 Part零件中。
        /// </summary>
        /// <param name="pInfo">货品信息</param>
        /// <returns>导入成功的Item ID</returns>
        public static string  AddPartFromPInfo(Common.PrdInfo pInfo)
        {
            string amlString = "<AML><Item type=\"Part\" action=\"add\">";
            amlString += "<keyed_name>" + pInfo.prd_no + "</keyed_name>";
            amlString += "<name>" + pInfo.prd_name + "</name>";
            amlString += "<description>" + pInfo.prd_spc + "</description>";
            amlString += "<xh_knd>" + pInfo.knd.ToString() + "</xh_knd>";
            amlString += "<xh_ut>" + pInfo.ut + "</xh_ut>";
            amlString += "<xh_ut1>" + pInfo.ut1 + "</xh_ut1>";
            amlString += "<xh_wh>" + pInfo.def_wh + "</xh_wh>";
            amlString += "<item_number>" + pInfo.prd_no + "</item_number>";
            amlString += "<xh_sum>" + pInfo.prd_name + "</xh_sum>";
            amlString += "<xh_dfu_ut>" + pInfo.qo_ut + "</xh_dfu_ut>";
            amlString += "<xh_chk_bat>" + pInfo.chk + "</xh_chk_bat>";
            amlString += "<xh_quote_ut2>" + pInfo.jl_ut + "</xh_quote_ut2>";
            amlString += "</Item></AML>";

            Item partItem = PublicState.ArasLoginInnovator.applyAML(amlString);
            if (!partItem.isEmpty() && !partItem.isError())
            {
                return partItem.getID().ToString();
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 增加PartBOM RelationShip 关系
        /// </summary>
        public static void AddRelationShip()
        {
            while (true)
            {
                Common.CRelationShip myship = new Common.CRelationShip();
                lock (PublicState.RelationShipList)
                {
                    if (PublicState.RelationShipList.Count > 0)
                    {
                        myship = PublicState.RelationShipList[0];
                        PublicState.RelationShipList.Remove(myship);
                    }
                }
                string haveString = "<AML><Item type=\"Part BOM\" action=\"get\" severEvents=\"0\">";
                haveString += "<source_id><Item type=\"Part\" action=\"get\">";
                haveString += "<id>" + myship.parentID + "</id></Item></source_id>";
                haveString += "<related_id><Item type=\"Part\" action=\"get\">";
                haveString += "<id>" + myship.childID + "</id></Item></related_id>";
                haveString += "</Item></AML>";
                Item reItem = PublicState.ArasLoginInnovator.applyAML(haveString);
                if (reItem.isEmpty() || reItem.isError())
                {
                    string amlStr = "<AML><Item type=\"Part BOM\" action=\"add\" severEvents=\"0\">";
                    amlStr += "<source_id><Item type=\"Part\" action=\"get\">";
                    amlStr += "<id>" + myship.parentID + "</id></Item></source_id>";
                    amlStr += "<related_id><Item type=\"Part\" action=\"get\">";
                    amlStr += "<id>" + myship.childID + "</id></Item></related_id>";
                    amlStr += "</Item></AML>";
                    PublicState.ArasLoginInnovator.applyAML(amlStr);
                }
            }
        }
    }
}
